package com.mixmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mixmall.entity.MaiProduct;
import com.mixmall.entity.MaiProductImage;
import com.mixmall.service.IMaiProductImageService;
import com.mixmall.service.IMaiProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caozhiren
 * @since 2024-09-13
 */
@RestController
@CrossOrigin
@RequestMapping("/mixmall/maiProduct")
public class MaiProductController {
    @Autowired
    IMaiProductService maiProductService;

    @Autowired
    IMaiProductImageService maiProductImageService;

    @RequestMapping("/list")
    @PreAuthorize("hasRole('admin')")
    @PostFilter("filterObject.sku==1")
    public Object getCateListById(Integer pid){
        QueryWrapper<MaiProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(pid!=null,MaiProduct::getProductId,pid);
        List<MaiProduct>  list = maiProductService.list(queryWrapper);

        QueryWrapper<MaiProductImage> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(pid!=null,MaiProductImage::getSkuId,pid);
        List<MaiProductImage> productImageList = maiProductImageService.list(queryWrapper2);

        Map<String,Object> result = new HashMap<>();
        result.put("productList",list);
        result.put("productImageList",productImageList);
        return result;
    }
}
